package com.yeci.common;

import cn.dev33.satoken.exception.NotLoginException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 全局异常处理
 * @author: yeci
 * @date: 2021/8/19 11:46
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

  @ResponseBody
  @ExceptionHandler(value = NotLoginException.class)
  public AjaxResult handleNotLoginException(NotLoginException nle, HttpServletRequest request, HttpServletResponse response) {

    // 判断场景值，定制化异常信息
    String message = "";
    if(nle.getType().equals(NotLoginException.NOT_TOKEN)) {
      message = "您还未登录，请先登录谢谢！！";
    }
    else if(nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
      message = "token无效,请重新登录！！";
    }
    else if(nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
      message = "token已过期,请重新登录！！";
    }
    else if(nle.getType().equals(NotLoginException.BE_REPLACED)) {
      message = "token已被顶下线,请重新登录！！";
    }
    else if(nle.getType().equals(NotLoginException.KICK_OUT)) {
      message = "token已被踢下线,请重新登录！！";
    }
    else {
      message = "当前会话未登录,请重新登录！！";
    }

    return AjaxResult.error(message);
  }

}
